package com.xingkeduo.pojo.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.ManagerQuery;
import com.xingkeduo.pojo.table.ManagerTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

public class ManagerJson extends BaseJson {

	public static ManagerQuery createQueryForManager(String json) throws Throwable {
		ManagerQuery query = new ManagerQuery();
		JsonNode root = validJson(json);
		if (root == null)
			return query;
		query.setId(validId(root, YesNoEnum.YES));
		return query;
	}

	public static ManagerTable createTableForModifyManager(String urlParam, ManagerTable managerTable) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");

		managerTable.setId(validId(root, YesNoEnum.YES));
		return managerTable;
	}

	public static ObjectNode toJson(ManagerTable managerTable) {
		ObjectNode root = MAPPER.createObjectNode();

		if (managerTable != null)
		{
			root.put("id", managerTable.getId());
			root.put("name", managerTable.getName());
//			root.put("password", managerTable.getPassword());
			root.put("realname", managerTable.getRealname());
            root.put("sex", managerTable.getSex());
			root.put("phone", managerTable.getPhone());
			root.put("email", managerTable.getEmail());
			root.put("deptmentId", managerTable.getDeptmentId());
			root.put("deptmentName", managerTable.getDeptmentName());
			root.put("postsId", managerTable.getPostsId());
			root.put("postsName", managerTable.getPostsName());
			root.put("citiesId", managerTable.getCitiesId());
            root.put("citiesName", managerTable.getCitiesName());
			root.put("type", managerTable.getType());
			root.put("status", managerTable.getStatus());
			root.put("isadmin", managerTable.getIsadmin());

			String updateTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(managerTable.getUpdatedTime());
			root.put("updatetime", updateTime);

			//处理时间
			String createTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(managerTable.getCreatedTime());
			root.put("createtime", createTime);
		}

		return root;
	}

	public static ArrayNode toJson(List<ManagerTable> managerList) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if(!CommonUtil.isEmpty(managerList)){
			for (Iterator<ManagerTable> iterator = managerList.iterator(); iterator.hasNext();) {
                root.add(toJson(iterator.next()));
            }
		}

		return root;
	}
	public static ObjectNode toJsonPage(Page<ManagerTable> page) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();

		if (page != null)
		{
			root.put("pageNo", page.getPageNo());
			root.put("pageSize", page.getPageSize());
			root.put("pageTotal", page.getPageTotal());
			root.put("recordTotal", page.getRecordTotal());
			root.put("dtoLists", toJson(page.getDtoList()));
		}
		return root;
	}

}
